/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.common.feedback.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.feedback.entity.SysImage;
import cn.apestar.modules.common.feedback.service.SysImageService;

/**
 * 任务图片Controller
 * @author lvh
 * @version 2016-07-10
 */
@Controller
@RequestMapping(value = "${adminPath}/task/sysImage")
public class SysImageController extends BaseController {

    @Autowired
    private SysImageService sysImageService;

    @ModelAttribute
    public SysImage get(@RequestParam(required = false) String id) {
        SysImage entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = sysImageService.get(id);
        }
        if (entity == null) {
            entity = new SysImage();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
        SysImage sysImage = new SysImage();
        sysImage.setDataId(request.getParameter("dataId"));
        List list = sysImageService.findList(sysImage);
        model.addAttribute("list", list);
        return "modules/task/sysImageList";
    }

    @RequestMapping(value = "form")
    public String form(SysImage sysImage, Model model) {
        model.addAttribute("sysImage", sysImage);
        return "modules/task/sysImageForm";
    }

    @RequestMapping(value = "save")
    public String save(SysImage sysImage, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, sysImage)) {
            return form(sysImage, model);
        }
        sysImageService.save(sysImage);
        addMessage(redirectAttributes, "保存任务图片成功");
        return "redirect:" + Global.getAdminPath() + "/task/sysImage/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(SysImage sysImage, RedirectAttributes redirectAttributes) {
        sysImageService.delete(sysImage);
        addMessage(redirectAttributes, "删除任务图片成功");
        return "redirect:" + Global.getAdminPath() + "/task/sysImage/?repage";
    }

}